/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.travel.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.sys.entity.Config;
import com.boxin.ims.modules.sys.utils.ConfigUtils;
import com.boxin.ims.modules.travel.entity.Bus;
import com.boxin.ims.modules.travel.service.BusService;
import com.boxin.ims.modules.wechat.entity.share.RuleType;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 车辆Controller
 * @author Jakemanse
 * @version 2013-07-31
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/travel/bus")
public class BusController extends BaseController {

	@Autowired
	private BusService busService;
	
	@ModelAttribute
	public Bus get(@RequestParam(required=false) Long id) {
		if (id != null){
			return busService.get(id);
		}else{
			return new Bus();
		}
	}
	
	@RequiresPermissions("travel:bus:view")
	@RequestMapping(value = {"list", ""})
	public String list(Bus bus, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			bus.setUser(user);
		}
        Page<Bus> page = busService.find(new Page<Bus>(request, response), bus); 
        model.addAttribute("page", page);
		return "modules/travel/busList";
	}

	@RequiresPermissions("travel:bus:view")
	@RequestMapping(value = "form")
	public String form(Bus bus, Model model) {
		if(bus.getUser() == null){
			bus.setUser(UserUtils.getUser());
		}
		
		bus.setQrcodeNumber(busService.generateQRCodeNumber());
		
		
		
		model.addAttribute("bus", bus);
		return "modules/travel/busForm";
	}
	
	
	@RequiresPermissions("travel:bus:view")
	@RequestMapping(value = "view/{id}"+Global.URL_SUFFIX)
	public String view(@PathVariable Long id ,  HttpServletRequest request, HttpServletResponse response) {
		Bus bus = busService.get(id);
		request.setAttribute("mybus", bus);
		request.setAttribute("mystr", "Just to ....");
		
		return "modules/travel/busViewForm";
	}

	@RequiresPermissions("travel:bus:edit")
	@RequestMapping(value = "save")
	public String save(Bus bus, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, bus)){
			
			return form(bus, model);
		}
		
		/**
		 * 验证是否已经超过随制数量
		 */
		User user = UserUtils.getUser();
		Bus queryBus = new Bus();
		queryBus.setUser(UserUtils.getUser());
		List<Bus> buses = busService.find( queryBus);
		Config config = ConfigUtils.getConfig(user.getId(), ConfigUtils.SERVICE_TYPE_TRAVEL_BUS);
		String value = (config == null) ? "30" : config.getValue();
		value = (StringUtils.isEmpty(value) ) ?  "0" : value;
		int intValue = Integer.parseInt(value);
		if(buses != null && buses.size() > intValue){	//超过指定的服务数量
			addMessage(model, "你已超过最大的使用数量 :"+value +",如需添加更多车辆请联系管理员!");
			return form(bus,model);
		}
		
		
		busService.save(bus);
		addMessage(redirectAttributes, "保存车辆'" + bus.getNumber() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/travel/bus/?repage";
	}
	
	@RequiresPermissions("travel:bus:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		busService.delete(id);
		addMessage(redirectAttributes, "删除车辆成功");
		return "redirect:"+Global.ADMIN_PATH+"/travel/bus/?repage";
	}

}
